﻿Imports Training.Common

Partial Public Class GroupTraining
    Inherits System.Web.UI.Page
    Public gName As String ' groupname
    Dim gid As Integer ' groupid

    ''' <summary>
    ''' Page load event, make sure querystring (qs) data is present
    ''' redirect if data is NOT present
    ''' assign qs data to variables
    ''' get group current module info
    ''' get module quizzes and videos
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsNothing(Request.QueryString("gid")) And Not IsNothing(Request.QueryString("nm")) Then
            gName = Request.QueryString("nm")
            gid = Request.QueryString("gid")

            ' load module name
            GetGroupModule()

            ' handle postbacks
            If Not Page.IsPostBack Then
                ' load data source for dropdown
                BindDropDown()
            End If
        Else
            Response.Redirect("Groups.aspx")
        End If
    End Sub

    ''' <summary>
    ''' Load Current ModuleName and bind repeaters
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub GetGroupModule()
        Dim dt As DataTable = GetModulesForGroup(gid)

        For Each row As DataRow In dt.Rows
            lblCurrent.Text = "Currently selected Module: <b>" & row("ModuleName") & "</b>" ' current modulename
            GetModuleQuizNames(row("ModuleID")) ' repeater for quizzes
            GetModuleVideoNames(row("ModuleID")) ' repeater for videos
        Next

    End Sub

    ''' <summary>
    ''' Bind data to repeater for quiz names
    ''' </summary>
    ''' <param name="mdid"></param>
    ''' <remarks></remarks>
    Private Sub GetModuleQuizNames(ByVal mdid As Integer)
        Dim dt As DataTable = GetModuleQuizzes(mdid)

        ' bind to repeater
        rptQuiz.DataSource = dt
        rptQuiz.DataBind()
    End Sub

    ''' <summary>
    ''' Bind data to repeater for video names
    ''' </summary>
    ''' <param name="mdid"></param>
    ''' <remarks></remarks>
    Private Sub GetModuleVideoNames(ByVal mdid As Integer)
        Dim dt As DataTable = GetModuleVideos(mdid)

        ' bind to repeater
        rptVideo.DataSource = dt
        rptVideo.DataBind()
    End Sub

    ''' <summary>
    ''' Binds Data to dropdownlist for modules
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub BindDropDown()
        ddlModules.DataSource = GetAllModules()
        ddlModules.DataTextField = "ModuleName"
        ddlModules.DataValueField = "ModuleID"
        ddlModules.DataBind()
    End Sub

    ''' <summary>
    ''' change button click event, change module for group
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnChange_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnChange.Click
        ' do the update
        If UpdateGroupModule(gid, ddlModules.SelectedValue) > 0 Then
            lblUpdate.Text = "<br/>Error Updating Group Module. Error has been logged."
        Else
            Response.Redirect("GroupTraining.aspx?gid=" & gid & "&nm=" & gName)
        End If
    End Sub

    ''' <summary>
    ''' Add Module Button Click Event
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>redirect to page to create modules</remarks>
    Private Sub btnAddModule_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddModule.Click
        Response.Redirect("AddModule.aspx")
    End Sub
End Class